﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class ToggleSwitchForm : ItemFormTemplate
    {
        public ToggleSwitchForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\ToggleSwitchForm\\ToggleSwitchForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.StylePattern = ToggleSwitchStylePattern.CircularZoom;
            this.demo_control.ReadOnly = false;
            this.demo_control.Toggleing += this.Demo_control_Toggleing;
            this.demo_control.Toggled += this.Demo_control_Toggled;

            this.demo_control_status.Text = "当前状态：" + (this.demo_control.IsOn ? this.demo_control.OnText : this.demo_control.OffText);

            this.style_dropDownListPlus.Multiple = false;
            this.style_dropDownListPlus.BindEnumList<ToggleSwitchStylePattern>(this.demo_control.StylePattern);
            this.style_dropDownListPlus.SelectedItemsChanged += this.StylePattern_dropDownListPlus_SelectedItemsChanged;

            this.readOnly_toggleSwitch.IsOn = this.demo_control.ReadOnly;
            this.readOnly_toggleSwitch.Toggled += this.ReadOnly_toggleSwitch_Toggled;

            this.offText_textBox.Text = this.demo_control.OffText;
            this.offText_textBox.LostFocus += this.OffText_textBox_LostFocus;
            this.onText_textBox.Text = this.demo_control.OnText;
            this.onText_textBox.LostFocus += this.OnText_textBox_LostFocus;
        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
            this.demo_control_status.Location = new Point(this.demo_control.Location.X, this.demo_control.Location.Y + this.demo_control.Height * 3);
        }

        private void Demo_control_Toggleing(object sender, CancelEventArgs e)
        {
            if (this.toggleing_toggleSwitch.IsOn)
            {
                if (MessageBox.Show("确定要更改状态", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private void Demo_control_Toggled(object sender, EventArgs e)
        {
            this.demo_control_status.Text = "当前状态：" + (this.demo_control.IsOn ? this.demo_control.OnText : this.demo_control.OffText);
        }

        private void StylePattern_dropDownListPlus_SelectedItemsChanged(object sender, EventArgs e)
        {
            DropDownListPlus control = (DropDownListPlus)sender;
            this.demo_control.StylePattern = (ToggleSwitchStylePattern)control.SelectedItem.Tag; ;
        }

        private void ReadOnly_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.ReadOnly = control.IsOn;
        }

        private void OffText_textBox_LostFocus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            this.demo_control.OffText = control.Text;
            this.offText_textBox.Text = this.demo_control.OffText;
        }

        private void OnText_textBox_LostFocus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            this.demo_control.OnText = control.Text;
            this.onText_textBox.Text = this.demo_control.OnText;
        }

        #endregion

    }
}
